// 1分钟内显示1分钟，1小时内显示多少分钟，1天内显示多少小时，1月内显示多少天，3月内显示多少月，超过3个月返回null
export function formatTimeDifference(timestamp) {
	const now = new Date();
	const inputDate = new Date(timestamp);

	// 检查输入是否有效
	if (isNaN(inputDate.getTime())) {
		return null;
	}

	// 计算两个日期之间的毫秒数差异
	const diffMilliseconds = now - inputDate;

	// 转换为秒、分钟、小时、天和月
	const diffSeconds = Math.abs(Math.floor(diffMilliseconds / 1000));
	const diffMinutes = Math.abs(Math.floor(diffSeconds / 60));
	const diffHours = Math.abs(Math.floor(diffMinutes / 60));
	const diffDays = Math.abs(Math.floor(diffHours / 24));
	const diffMonths = Math.abs(Math.floor(diffDays / 30.44)); // 平均每月按30.44天计算

	if (diffMonths < 3) {
		if (diffMinutes < 1) {
			return '1分钟';
		} else if (diffHours < 1) {
			return `${diffMinutes}分钟`;
		} else if (diffDays < 1) {
			return `${diffHours}小时`;
		} else if (diffMonths < 1) {
			return `${diffDays}天`;
		} else {
			return `${diffMonths}月`;
		}
	} else {
		return null;
	}
}

// 前往首页
export function gotoHome() {
	uni.showModal({
		title: "提示",
		content: "页面有误将返回首页",
		showCancel: false,
		success(res) {
			if (res.confirm) {
				uni.reLaunch({ url: "/pages/index/index" })
			}
		}
	})
}